<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/css/base.css">
</head>
<body>
  <h2>home页</h2>
  <% if(items.length === 0) { %>
    <h2>暂无商品</h2>
  <%}%>
  <ul>
    <% for(var i=0;i<items.length;i++) { %>
    <li>
      <h4>商品名字:<%= items[i].itemName %></h4>
      <p>商品价格: <%= items[i].itemPrice %></p>
      <p>上下架： <%= items[i].onSale === 1?'上架':'下架' %></p>
      <button data-id="<%= items[i]._id %>" class="update">修改商品</button>
      <button data-id="<%= items[i]._id %>" class="del">删除商品</button>
    </li>
    <% } %>
  </ul>
  <script src="/static/lib/jquery-3.6.0.js"></script>
  <script>
    // 更新
    $('.update').click(function(){
      $.ajax({
        url: '/updateItem',
        type: 'post',
        data: {
          id: this.dataset.id
        },
        success: function(res){
          if(res.code === 0) {
            alert(res.msg)
            // 刷新页面
            history.go(0);
          }
        }
      })
    })
    // 删除商品
    $('.del').click(function(){
      // 删除一定要给确认框
      if(confirm('您确认删除吗?')){
        $.ajax({
          url: '/delItem',
          data: {
            id: this.dataset.id
          },
          success: function(res){
            if(res.code ===0){
              alert(res.msg);
              history.go(0)
            }
          }
        })
      }
      
    })
  </script>
</body>
</html>